<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<head>
<style>

/*功能区按钮块样式*/
.pl_809_btn_div > div{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-top:5px;
}

.pl_809t_vehSlct_div{
    width:680px;
    height:510px;
    border-radius:4px;
    border:1px solid rgb(210,210,210);
    background-color: white;
    z-index: 100;
    position: absolute;
    left:160px;
    top:50px;
    padding: 10px;
}
.pl_809t_vehSlct_div > div{
    width:100%;
}
    
</style>


<script>

//初始化控件,页面加载时即加载，不需要调用
$(function(){
    //初始化表格
    $("#pl_809trans_grid").datagrid({
        width:"100%",
        height:"100%",
        // pagination:true,//分页工具栏
		// pageList:[15,30,50,100],
		// pageSize:15,
		singleSelect:true,//单选
		rownumbers:true,//行号
		striped:true,//隔行变色
		fitColumns: false,//列的尺寸自动缩放
		checkOnSelect: false,//选中则勾选
		selectOnCheck: false,//勾选则选中
    });

    $("#pl_809trans_vehSlct_grid").datagrid({
        width:"100%",
        height:"100%",
        toolbar:"#pl_809trans_vehSlct_grid_toolbar",//工具条
        // pagination:true,//分页工具栏
		// pageList:[15,30,50,100],
		// pageSize:15,
		singleSelect:true,//单选
		rownumbers:true,//行号
		striped:true,//隔行变色
		fitColumns: false,//列的尺寸自动缩放
		checkOnSelect: false,//选中则勾选
		selectOnCheck: false,//勾选则选中
    });
});

//////////////////////////////////////////////////////////////变量
var pl_809trans = {};
pl_809trans.debug = true;
pl_809trans.slctUrl = globalConfig.webPath + '/basicSlctAction/basicSelectData.action';
pl_809trans.slctAuthUrl = globalConfig.webPath + '/basicSlctAction/selectDataByAuth.action';

pl_809trans.cache = {};//缓存
pl_809trans.cache.transParam = {};

pl_809trans.transCache = {};//转发功能所需缓存
pl_809trans.transCache.index = -1;//当前已经转发过的车辆数据
pl_809trans.transCache.transFlg = false;//转发标记，标明是否允许转发
pl_809trans.transCache.totalNum = 0;//当前需要转发的车辆数
pl_809trans.transCache.num = 0;//开始进行转发的车辆数
pl_809trans.transCache.succNum = 0;
pl_809trans.transCache.errNum = 0;
pl_809trans.transCache.rowData = {rows:[]};//缓存车辆数据
pl_809trans.transCache.transUrl = globalConfig.webPath + "/insertAndTrans/transOneVehGpsData.action";

//////////////////////////////////////////////////////////////初始化控件
pl_809trans.initDivs = function(){
    this.initPlatformNameDiv();
    this.initVehSlctDivs();
}

//初始化转发平台名称选择控件
pl_809trans.initPlatformNameDiv = function(){
    DataUtil.initSlctDivByBasicData('pl_809trans_platformName','Jt809LinkName');
}

//初始化车辆导入相关控件
pl_809trans.initVehSlctDivs = function(){
    this.initDepDiv();
}
//初始化车组控件
pl_809trans.initDepDiv = function(){
//创建下拉部门树
    Utility.createDepTree("pl_809trans_vehSlct_depId");
}


//////////////////////////////////////////////////////////////初始化数据
//初始化数据
pl_809trans.initDatas = function(){
    this.initTransVehiclesData();
    this.initTransParamData();
}

//初始化车辆转发信息数据
pl_809trans.initTransVehiclesData = function(){
    let that = this;
    let param = {
        queryId:'jt809Command.getAllTransVehDatas',
    };
    Utility.getAjaxData(this.slctUrl,JSON.stringify(param),function(data){
        that.outDebug("接收到转发定位信息车辆数据：" , data);
        if(data.success){
            $("#pl_809trans_grid").datagrid('loadData',data.data);
        }else{
            DataUtil.showError(data.message);
        }
    });
}

//初始化转发参数数据
pl_809trans.initTransParamData = function(){
    try {
        let param = ${transParam};
        if(!DataUtil.isDefined(param)){
            if(param.name != "" ){
                $("#pl_809trans_platformName").combobox('setValue',param.name);
                if(param.startTime != ""){
                    $("#pl_809trans_startDate").datetimebox('setValue',param.startTime);
                }
                if(param.endTime != ""){
                    $("#pl_809trans_endDate").datetimebox('setValue',param.endTime);
                }
            }
        }
    } catch (error) {
        
    }
    
}


//////////////////////////////////////////////////////////////转发功能
//开始转发按钮事件
pl_809trans.startTransBtnClick = function(){
    let that = this;
    let param = this.getTransData();
    this.checkTransData(param);
    if(param.message == ""){
        this.outDebug("转发的数据：",param);
        let str = "转发平台：" + param.name + " ，开始时间： " + //转发信息
        param.startDate + " , 结束时间：" + param.endDate + " ， 需要转发的车辆数量为：" + param.sendNum + " 辆。";
        this.outMsg("当前数据合法，允许转发。" + str);
        $.messager.confirm("转发","是否进行转发？转发数据为：" + str,function(res){
            if(res){
                that.startTransService(param);
            }
        });
    }else{
        this.outMsg(" 当前无法进行转发：" + param.message ,'red');
        DataUtil.showErrorMsg(" 当前无法进行转发：" + param.message ,'red');
    }
}

//获取转发数据
pl_809trans.getTransData = function(){
    let param = DataUtil.getParamFromForm('pl_809trans_trandDataForm');
    param.rowData = $("#pl_809trans_grid").datagrid('getData');//转发数据
    param.sendNum = 0;
    return param;
}

//校验转发数据
pl_809trans.checkTransData = function(param){
    if(param.message == ""){
        if(DataUtil.isDefined(param.name)){
            param.message += " 请选择转发平台!";
        }
        if(DataUtil.isDefined(param.startDate)){
            param.message += " 请选择转发数据开始时间!";
        }if(DataUtil.isDefined(param.endDate)){
            param.message += " 请选择转发数据结束时间!";
        }
        if(param.rowData && param.rowData.rows && param.rowData.rows.length> 0){
            let sendNum = 0;
            $.each(param.rowData.rows,function(n,item){
                if(item.status == '0'){
                    sendNum ++;
                }
            });
            if(sendNum > 0){
                param.sendNum = sendNum;
            }else{
                param.message += " 当前列表中没有需要转发的数据!";
            }
        }else{
            param.message += " 需要转发的数据不存在!";
        }
    }
}

//开启转发服务
pl_809trans.startTransService = function(param){
    this.initTransCache(param);
    this.outMsg("开始进行转发........");
    this.transOneVehGpsData(param);
}

//初始化转发缓存
pl_809trans.initTransCache = function(param){
    //初始化
    this.transCache.index = -1;
    this.transCache.totalNum = param.sendNum;
    this.transCache.transFlg = true;
    this.transCache.num = 0;
    this.transCache.succNum = 0;
    this.transCache.errNum = 0;
    this.transCache.rowData = param.rowData;
}

//转发一个车辆GPS数据
pl_809trans.transOneVehGpsData = function(param){
    let that = this;
    if(that.transCache.transFlg == true){
        that.transCache.num ++ ;
        let veh = that.getNextTransData();
        if(veh != null){
            let map = {
                plateNo:veh.plateNo,
                plateColor:veh.plateColor,
                name:param.name,
                startDate:param.startDate,
                endDate:param.endDate
            };
            that.outMsg("转发车辆，序列号：" + that.transCache.num + " ,数据：" + DataUtil.obj2String(map));
            Utility.getAjaxData(that.transCache.transUrl,JSON.stringify(map),function(data){
                if(data.success){
                    that.transCache.succNum ++;
                    that.outMsg("本次车辆gps数据转发成功,GPS定位数据数量："+ data.data.gpsNum);
                    that.updateTransResult(1,"转发成功",data.data.transDate);
                }else{
                    that.transCache.errNum ++;
                    that.outMsg("本次车辆gps数据转发失败，GPS定位数据数量：" + data.data.gpsNum + "，失败原因" + data.message);
                    that.updateTransResult(2,"转发失败",data.data.transDate);
                }
                that.transOneVehGpsData(param);//转发下一辆
            });
        }else{
            if(that.transCache.num <= that.transCache.totalNum){
                that.outMsg("转发车辆，序列号：" + that.transCache.num + " ，没有获取到车辆转发车辆数据;");
                that.transCache.errNum ++;
                that.transOneVehGpsData(param);//转发下一辆
            }else{
                that.outMsg("转发车辆完成，共计转发车辆：" + (that.transCache.num - 1) + "辆，成功："//统计信息
                    + that.transCache.succNum + "辆，失败：" + that.transCache.errNum + " 辆");
                that.outMsg("--------------------------------");
                that.outMsg("");
            }
        }
    }else{
        that.outMsg("停止转发！");
        that.outMsg("--------------------------------");
        that.outMsg("");
    }
}

//获取下一个需要转发的数据
pl_809trans.getNextTransData = function(){
    let result = null;
    let cache = this.transCache;
    cache.index ++;
    if(cache.index < cache.rowData.rows.length){
        if(cache.rowData.rows[cache.index].status == '0'){
            result =  cache.rowData.rows[cache.index];
            return result;
        }else{
            result = this.getNextTransData();//递归获取可以转发的车辆
        }
    }
    return result;
}

//更新转发结果
pl_809trans.updateTransResult = function(status,remark,transDate){
    let that = this;
    try {
        let cache = that.transCache;
        if(cache.index > -1 && cache.index < cache.rowData.rows.length){
            cache.rowData.rows[cache.index].status = status;
            cache.rowData.rows[cache.index].remark = remark;
            cache.rowData.rows[cache.index].transDate = transDate;
            $("#pl_809trans_grid").datagrid('loadData',cache.rowData);
            $("#pl_809trans_grid").datagrid('scrollTo',cache.index);

        }
    } catch (error) {
        
    }
}




//停止转发按钮事件
pl_809trans.stopTransBtnClick = function(){
    this.outMsg("开始停止转发........");
    this.transCache.transFlg = false;
}


//////////////////////////////////////////////////////////////导入转发车辆数据操作

//导入转发车辆数据
pl_809trans.insertSlctedVeh = function(rows){
    let that =  this;
    let url = globalConfig.webPath + "/platformFun/insertSlctedVehForTrans.action";
    let param = {
        createDate:Utility.now(),
        rows:rows,
    };
    that.outMsg("开始导入数据，耐心等待.......");
    Utility.getAjaxData(url,JSON.stringify(param),function(data){
        // that.outDebug('接收到导入转发车辆响应数据：',data);
        if(data.success){
            that.outMsg("导入数据成功！")
        }else{
            that.outMsg(data.message);
            DataUtil.showErrorMsg(data.message);
        }
        //刷新车辆转发信息表
        that.initTransVehiclesData();
    });
}


//导入车辆界面查询按钮事件
pl_809trans.vehEditDivQueryFun = function(){
    let that = this;
    let param = this.vehEditGetQueryParam();
    if(param.message == ""){
        // that.outDebug('查询导入车辆参数：',param);
        Utility.getAjaxData(that.slctAuthUrl,JSON.stringify(param),function(data){
            // that.outDebug("接收到导入车辆数据：" , data);
            if(data.success){
                $("#pl_809trans_vehSlct_grid").datagrid('loadData',data.data);
                if(data.data.length <= 0 ){
                    DataUtil.showMsg("当前车辆数为0!");
                }
            }else{
                DataUtil.showErrorMsg(data.message);
            }
        })
    }else{
        DataUtil.showErrorMsg("无法进行查询：" + param.message);
    }
}

//获取查询参数
pl_809trans.vehEditGetQueryParam = function(){
    let param = {
        message:"",
        queryId:"platform.getAllVehByVehList",
    };
    let depId = $("#pl_809trans_vehSlct_depId").combobox('getValue');
    if(!DataUtil.isDefined(depId)){
        if(depId != '0'){
            param.depId = depId;
        }
    }
    param.plateNo = $("#pl_809trans_vehSlct_plateNo").val();
    return param;
}


//确定按钮事件
pl_809trans.vehEditDivOkFun = function(){
    let that =  this;
    let rows = $("#pl_809trans_vehSlct_grid").datagrid('getChecked')
    if(rows.length > 0){
        $.messager.confirm("询问","确定导入" + rows.length +"个车辆数据到车辆转发信息表中？",function(res){
            if(res){
                // that.outDebug("选中的数据为：",rows);
                that.insertSlctedVeh(rows);
                //退出
                that.vehEditDivCancelFun();
            }
        });
    }else{
        that.vehEditDivCancelFun();
    }
}


//导入转发车辆数据事件
pl_809trans.vehEditDivShowFun = function(){
    let that = this;
    $("#pl_809trans_vehSlct_grid").datagrid('loadData',[]);
    let obj = document.getElementById('pl_809trans_vehEdit_div');
    if(obj != null){
        obj.style.zIndex = '100';
        obj.style.display = '';
        that.vehEditDivQueryFun();
    }
}


//车辆数据选择表取消事件
pl_809trans.vehEditDivCancelFun = function(){
    let obj = document.getElementById('pl_809trans_vehEdit_div');
    if(obj != null){
        obj.style.zIndex = '-100';
        obj.style.display = 'none';
    }
}


//////////////////////////////////////////////////////////////车辆信息表其他操作
//导入所有车辆
pl_809trans.insertAllTransVeh = function(){
    let that = this;
    $.messager.confirm("询问","确定导入所有车辆到车辆转发信息表？",function(res){
        if(res){
            let param = {
                message:"insertAll",
                queryId : "platform.insertAllVehForTrans",
            };
            that.outMsg("开始导入所有车辆。。。。。");
            Utility.getAjaxData(that.slctUrl,JSON.stringify(param),function(data){
                if(data.success){
                    that.outMsg("导入所有车辆成功！");
                }else{
                    that.outMsg(data.message);
                    DataUtil.showErrorMsg(data.message);
                }
                that.initTransVehiclesData();//刷新车辆数据表
            });
        }
    });
}

//清空转发信息表
pl_809trans.removeAllTransVeh = function(){
    let that = this;
    $.messager.confirm("询问","确定清空转发信息表？",function(res){
        if(res){
            let param = {
                message:"removeAll",
                queryId : "platform.removeAllVehForTrans",
            };
            that.outMsg("开始清空车辆信息表。。。。。");
            Utility.getAjaxData(that.slctUrl,JSON.stringify(param),function(data){
                if(data.success){
                    that.outMsg("清空车辆信息表成功！");
                }else{
                    that.outMsg(data.message);
                    DataUtil.showErrorMsg(data.message);
                }
                that.initTransVehiclesData();//刷新车辆数据表
            });
        }
    });
}




//////////////////////////////////////////////////////////////格式化数据
pl_809trans.formatStatusValue = function(value,rowData,rowIndex){
    let str = '';
    switch(value){
        case 1:
            str = '<span style="color:green;">成功</span>'
            break;
        case 2:
            str = '<span style="color:red;">失败</span>'
            break;
        default :
            str = '待转发';
            break;
    }
    return str;
}



//////////////////////////////////////////////////////////////公共方法
//输出调试信息
pl_809trans.outDebug = function(msg,data){
    if(this.debug == true){
        DataUtil.outDebug(msg,data);
    }
}

//显示信息到控件中
pl_809trans.outMsg = function(msg,color){
    let colorStr = '';
    if(color == 'red'){
        colorStr = 'style="color:red;"';
    }
    let html = '<div ' + colorStr + '>';
    html += '&nbsp;&nbsp;&nbsp;&nbsp;[' + Utility.now() + ']&nbsp;&nbsp;';
    html += msg;
    html += '</div>';
    $("#pl_809trans_msgDiv").prepend(html);
}

//////////////////////////////////////////////////////////////加载后执行    
$(document).ready(function(){
    pl_809trans.initDivs();
    pl_809trans.initDatas();
});
    
</script>


</head>
<body style="width:98%;height:96%;" class="sh_css_basFontSize">
    <div style="width:100%;height:100%;" class="base_hori_flexStartDiv_css">
        <!--左侧转发信息表-->
        <div style="width:530px;height:100%;" class="base_ver_flexStartDiv_css">
            <div style="margin-top:10px;">
                车辆转发信息表
                <a href="javascript:pl_809trans.initTransVehiclesData();">刷新</a>
            </div>
            <div style="width:98%;height:93%;margin-top:10px;border-radius: 3px;">
                <table id="pl_809trans_grid" class="easyui-datagrid" style="width:100%;">
                    <thead>
                        <tr>
                            <th data-options="field:'id',checkbox:true"><input type='checkbox' id='selectAll'></input></th>
                            <th data-options="field:'depName'" width="80">车组</th>
                            <th data-options="field:'plateNo'" width="70">车牌号</th>
                            <th data-options="field:'status',formatter:pl_809trans.formatStatusValue" width="70">状态</th>
                            <th data-options="field:'transDate'" width="130">转发时间</th>
                            <th data-options="field:'remark'" width="120">备注</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
        <!--右侧功能区-->
        <div style="width:560px;height:100%;margin-left:5px;">
            <div style="width:100%;margin-top:40px;"> 
                转发设置区--------------------
            </div>
            <div style="width:100%;height:65px;margin-top:10px;" class="base_ver_Around_leftDiv_css pl_809_btn_div">
                <div>
                    <form id="pl_809trans_trandDataForm">
                    <span>转发平台：</span>
                    <select name="name" id="pl_809trans_platformName" style="width:90px;"></select>
                    &nbsp;&nbsp;
                    <span>起止时间：</span>
                    <input  name="startDate" id="pl_809trans_startDate" class="easyui-datetimebox" style="width:150px;">
                    <span>-</span>
                    <input  name="endDate" id="pl_809trans_endDate" class="easyui-datetimebox" style="width:150px;">
                    </form>
                </div>
                <div style="margin-left:170px;margin-top:5px;">
                    <a id="pl_809trans_BtnTransStop" href="#" class="easyui-linkbutton"  onclick="pl_809trans.stopTransBtnClick();"
                    data-options="iconCls:'icon-cancel'">停止转发</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <div id="pl_809trans_BtnTransStart" class="sh_css_btn" style="margin-left:15px;" onclick="pl_809trans.startTransBtnClick();">
                        <div class="ui-icon icon-ok" style="width:16px;height:16px;"></div> 
                        <span>开始转发</span>
                    </div>
                </div>
            </div>
            <div style="width:100%;margin-top:15px;">
                表格操作区--------------------
            </div>
            <div style="width:100%;height:35px;margin-top:10px;" class="base_ver_Around_leftDiv_css pl_809_btn_div">
                <div>
                    <a href="#" class="easyui-linkbutton" onclick="pl_809trans.vehEditDivShowFun();">导入转发车辆数据</a>
                    &nbsp;&nbsp;
                    <a href="#" class="easyui-linkbutton" onclick="pl_809trans.insertAllTransVeh();">导入所有车辆</a>
                    &nbsp;&nbsp;
                    <a href="#" class="easyui-linkbutton" onclick="pl_809trans.removeAllTransVeh();">清空转发信息表</a>
                </div>
            </div>
            <div style="width:100%;margin-top:10px;">
                操作日志--------------------
            </div>
            <div id="pl_809trans_msgDiv"
            style="width:100%;height:60%;margin-top:10px;border:1px solid rgb(210,210,210);border-radius:4px;overflow: auto;">

            </div>
        </div>
        <!--导入车辆操作控件-->
        <div id="pl_809trans_vehEdit_div" class="pl_809t_vehSlct_div base_ver_spaceBetweenDiv_css" style="z-index:-100;display:none;">
            <span style="font-size:15px;">车辆数据选择表</span>
            
            <div style="width:96%;height:420px;border:1px solid rgb(200,200,200);border-radius:4px;">
                <div id="pl_809trans_vehSlct_grid_toolbar" style="padding:8px;width:100%;" class="base_hori_flexStartDiv_css">
                    <span>车组:</span>
                    <select id="pl_809trans_vehSlct_depId" name="depId" style="width:120px;"></select>
                    &nbsp;
                    <span>车牌号:</span>
                    <input type="text" id="pl_809trans_vehSlct_plateNo" style="width:80px;">
                    &nbsp;
                    <a href="#" class="easyui-linkbutton" onclick="pl_809trans.vehEditDivQueryFun();" style="width:90px;">查询</a>
                </div>
                <table id="pl_809trans_vehSlct_grid" class="easyui-datagrid" style="width:100%;">
                    <thead>
                        <tr>
                            <th data-options="field:'id',checkbox:true"><input type='checkbox' id='selectAll'></input></th>
                            <th data-options="field:'depName'" width="120">车组</th>
                            <th data-options="field:'plateNo'" width="90">车牌号</th>
                            <th data-options="field:'plateColor'" width="80">车牌颜色</th>
                            <th data-options="field:'remark'" width="230">备注</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="base_hori_flexCenterDiv_css">
                <button onclick="pl_809trans.vehEditDivOkFun();">确定</button>
                <button style="margin-left:30px;" onclick="pl_809trans.vehEditDivCancelFun();">取消</button>
            </div>
        </div>
    </div>
</body>